//
//  AppDelegate+WXHandle.m
//  网易新闻
//
//  Created by Yeewii@hotmail.com on 16/6/21.
//  Copyright © 2016年 Yeewii@hotmail.com. All rights reserved.
//

#import "AppDelegate+WXHandle.h"
#import <MJExtension.h>
#import "YWUserTool.h"


static NSString *wxappID          = @"wxad33935a73e9c752";
static NSString *wxAppSecret      = @"7f1b3a430fc369f88f72abd5b040bd0d";
@implementation AppDelegate (WXHandle)

-(void)onResp:(BaseReq *)resp
{

    SendAuthResp *aresp = (SendAuthResp *)resp;
    if (aresp.errCode== 0)
    {
        NSLog(@"用户同意授权");
        
        NSString *code = aresp.code;
                [self getOpenID:code];
    }else if (aresp.errCode == -2)
    {
        NSLog(@"用户取消授权");
    }else if (aresp.errCode == -4)
    {
        NSLog(@"用户拒绝授权");
    }
    
}
-(void)getOpenID:(NSString *)code
{
    
    
    
    NSString *url =[NSString stringWithFormat:@"https://api.weixin.qq.com/sns/oauth2/access_token?appid=%@&secret=%@&code=%@&grant_type=authorization_code",wxappID,wxAppSecret,code];
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSURL *zoneUrl = [NSURL URLWithString:url];
        NSString *zoneStr = [NSString stringWithContentsOfURL:zoneUrl encoding:NSUTF8StringEncoding error:nil];
        NSData *data = [zoneStr dataUsingEncoding:NSUTF8StringEncoding];
        dispatch_async(dispatch_get_main_queue(), ^{
            if (data) {
                NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
                NSString *accessToken = [dic objectForKey:@"access_token"];
                NSString *openID = [dic objectForKey:@"openid"];
                NSString *userInfo = [NSString stringWithFormat:@"https://api.weixin.qq.com/sns/userinfo?access_token=%@&openid=%@",accessToken,openID];
                [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:userInfo]] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError)
                 {
                     NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
                     //                     NSLog(@"%@",dic);
                     NSString *country = dic[@"country"];
                     NSString *headimgurl = dic[@"headimgurl"];
                     NSString *nickname = dic[@"nickname"];
                     NSString *openid = dic[@"openid"];
                     NSString *sex = dic[@"sex"];
                     [YWUserTool SetYWObject:country forKey:@"country"];
                     [YWUserTool SetYWObject:headimgurl forKey:@"headimgurl"];
                     [YWUserTool SetYWObject:nickname forKey:@"nickname"];
                     [YWUserTool SetYWObject:openid forKey:@"openid"];
                     [YWUserTool SetYWObject:sex forKey:@"sex"];
                     YWWeChatModel *mod = [YWWeChatModel new];
                     mod.country = country;
                     mod.headimgurl = headimgurl;
                     mod.nickname = nickname;
                     mod.openid = openid;
                     mod.sex = sex;
                     
                     [[NSNotificationCenter defaultCenter]postNotificationName:kUserMessageNotification object:mod];
                     
                 }];
                
            }
        });
    });
}


-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    return [WXApi handleOpenURL:url delegate:self];
}
@end
